<?php
/*
文件上传、图片压缩
Created by Reon
@Category	Library
@Library	Upload
*/
class RPHP_Upload
{
	
	//上载文件
	function up($file,$newfile,$max_size=false,$types=false,$index = -1){
		if($index>=0){
		$tmp_file['name'] = $file['name'][$index];
		$tmp_file['size'] = $file['size'][$index];
		$tmp_file['type'] = $file['type'][$index];
		$tmp_file['tmp_name'] = $file['tmp_name'][$index];
		}else{
		$tmp_file['name'] = $file['name'];
		$tmp_file['size'] = $file['size'];
		$tmp_file['type'] = $file['type'];
		$tmp_file['tmp_name'] = $file['tmp_name'];
		}		
		if($tmp_file['name']!=''){
			if($tmp_file['size']<$max_size||$max_size==false){
				if(!$types  || @in_array($tmp_file['type'],$types)){
					if(move_uploaded_file($tmp_file['tmp_name'],$newfile)){
					return array('success'=>true);
					}else{
					return array('success'=>false,'error'=>'无法拷贝文件到指定目录，请重试');
					}
				}else{
				return array('success'=>false,'error'=>'文件格式不在允许范围内，请重新选择');
				}
			}else{		
			return array('success'=>false,'error'=>'文件大小超过上传限制，请重新选择');
			}
			@unlink($tmp_file['tmp_name']);
		}
	}
	
	
	//缩放图片
	function mt($srcFile,$dstFile,$max_width=140,$max_height=105,$imgQuality=90){
	  $data=@getimagesize($srcFile);
	  if($data[0]<=$data[1] and $data[1]>=$max_height){
		 $height=$max_height;
		 $width=intval($height*$data[0]/$data[1]);
	  }
	  if($data[0]>=$data[1] and $data[0]>=$max_width){
		 $width=$max_width;
		 $height=intval($width*$data[1]/$data[0]);
	  }
	  if($height>$max_height){
		 $height=$max_height;
		 $width=intval($height*$data[0]/$data[1]);
	  }
	  if($data[0]<$max_width and $data[1]<$max_height){
		 $width=$data[0];
		 $height=$data[1];
	  }
			   switch($data[2]){
					  case 1:
						   $im=@imagecreatefromgif($srcFile);
						   break;
					  case 2:
						   $im=@imagecreatefromjpeg($srcFile);
						   break;
					  case 3:
						   $im=@imagecreatefrompng($srcFile);
						   break;
			   }
			   $srcW=@imagesx($im);
			   $srcH=@imagesy($im);
			   $ni=@imagecreatetruecolor($width,$height);

                $color = imagecolorallocate($ni,255,255,255);


                imagefill($ni,0,0,$color);
			   @imagecopyresampled($ni,$im,0,0,0,0,$width,$height,$srcW,$srcH);
				switch($data[2]){
					case 'gif':@imagepng($ni,$dstFile, $imgQuality); break;
					case 'jpeg':@imagejpeg($ni,$dstFile, $imgQuality); break;
					case 'png':@imagepng($ni,$dstFile, $imgQuality); break;
					default:@imagejpeg($ni,$dstFile, $imgQuality); break;
				}
	  }
	  
	  //按比例缩放图片宽度高度
	  function rs($pic,$max_width=200,$max_height=200){
		 $data=@getimagesize($pic);
		  if($data[0]<=$data[1] and $data[1]>=$max_height){
			 $height=$max_height;
			 $width=intval($height*$data[0]/$data[1]);
		  }
		  if($data[0]>=$data[1] and $data[0]>=$max_width){
			 $width=$max_width;
			 $height=intval($width*$data[1]/$data[0]);
		  }
		  if($height>$max_height){
			 $height=$max_height;
			 $width=intval($height*$data[0]/$data[1]);
		  }
		  if($data[0]<$max_width and $data[1]<$max_height){
			 $width=$data[0];
			 $height=$data[1];
		  }
		  return array($width,$height); //返回以宽度，高度为数组的结果
	}
	
	//返回文件的格式名称
	function rf($str,$up=false){
	$str=explode(".",$str);
	if($up) return strtoupper($str[count($str)-1]);
	else return strtolower($str[count($str)-1]);
	}
	
	//文件大小计算
	function gs($size,$unit=1024){
		if($size>$unit){
			$kb = number_format($size/$unit,2,".","");
			if($kb>$unit){
			return number_format($kb/$unit,2,".","").'MB';
			}else{
			return $kb.'KB';
			}
		}else{
		return $size.'B';
		}
	}
}
?>